﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;


public partial class ClientLogout : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Client client;
    List<Client> clients;

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        if (cookie == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (!Page.IsPostBack)
            {
                if (Convert.ToInt32(cookie["AdminId"]) == 0 || Convert.ToInt32(cookie["AdminId"]) == -1)
                {
                    Response.Redirect("Login.aspx");
                }


                bindClient();
            }
        }
    }

    private void bindClient()
    {
        client = new Client(_dataObject);
        HttpCookie cookie = Request.Cookies["Admin"];
        clients = new List<Client>();
        if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
        {
            int AdminId = Convert.ToInt32(cookie["AdminId"]);
            List<GetAllLoginByAdminResult> lstClient = new List<GetAllLoginByAdminResult>();
            lstClient = client.GetAllLoginByAdmin(AdminId);
            if (lstClient.Count > 0)
            {
                rptClient.DataSource = lstClient;
                rptClient.DataBind();
            }
        }
        else
        {
            List<GetAllLoginClientsResult> lstResult = new List<GetAllLoginClientsResult>();
            lstResult = client.GetAllLoginClient();
            if (lstResult.Count > 0)
            {
                rptClient.DataSource = lstResult;
                rptClient.DataBind();
            }
            else
            {
                rptClient.DataSource = null;
                rptClient.DataBind();
            }
        }
    }
    protected void rptClient_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
        if (e.CommandName.ToString().ToLower().Trim().Equals("logout"))
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnClientId = (System.Web.UI.WebControls.Label)row.FindControl("hdnClientId");
            client = new Client(_dataObject);
            client.Id = Convert.ToInt32(hdnClientId.Text);
            client.IsLogin = false;
            client.UpdateIsLoginForClient();
            divRed.InnerHtml = "<button data-dismiss='alert' class='close'></button><strong>Success! </strong>&nbsp;Client Logged successfully!";
            divRed.Attributes.Add("class", "alert alert-success");
            bindClient();
        }
    }

    protected void btnAllLogout_Click(object sender, EventArgs e)
    {

        client = new Client(_dataObject);
        client.LogoutAllClient();
        Response.Redirect("ClientLogout.aspx");
    }
}